// pages/mall/orderDetail/orderDetail.js
import * as API_Mall_Order from '../../../api/mall/order'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mesh:{},
    productList:[],
    address:{},
    title:'',
    meshId:'',
    showAddr:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      meshId:options.id
    })
    this.loadData()
  },
  queryState(e){
    let id=e.currentTarget.dataset.id;
    let that=this;
    wx.navigateTo({
      url: '../orderLogistics/orderLogistics?id='+id,
    })
  },
  confirmShipped(e){
  let id=e.currentTarget.dataset.id;
    let that=this;
    wx.showModal({
      title:'提示',
      content:'是否确认收货？',
      success (res) {
        if (res.confirm) {
          API_Mall_Order.confirmShipped(id).then(res=>{
            setTimeout(() => {
              that.loadData()
            }, 1500);
          })
          // that.cancel(id);
        }
      }
    })
   

  },
  receive(){
    let that = this
    let meshId = this.data.meshId
    wx.showModal({
      title:'提示',
      content:'是否确认收货？',
      success (res) {
        if (res.confirm) {
          API_Mall_Order.confirmAll(meshId).then(res=>{
            wx.showToast({
              title: '收货成功',
              icon:'success',
              duration:1500
            })
            setTimeout(() => {
              that.loadData()
            }, 1500);
          })
          // that.cancel(id);
        }
      }
    })
   
  },
  toPay:function(){
    let that=this;
    let params={orderId:this.data.meshId};
    API_Mall_Order.delayPay(params).then(res=>{
      if(res.code==200){

        let payment=res.data;
      if(payment.status=="succeeded"){
        let expend=payment.expend;
        let pay_info=JSON.parse(expend.pay_info);

        wx.requestPayment(
          {
              'timeStamp':pay_info.timeStamp,
              'nonceStr': pay_info.nonceStr,
              'package': pay_info.package,
              'signType': pay_info.signType,
              'paySign': pay_info.paySign,
              'success':function(res){
                wx.showToast({
                  title: '支付成功',
                })
                setTimeout(()=>{
                    that.loadData()
                }, 1500)

                },
                'fail':function(res){
                  
                },
                'complete':function(res){
                }
          })
      }
      }else{
        that.setData({payState:false});
        wx.showToast({
          title: res.msg
        })
      }

    })

  },

  loadData(){
    API_Mall_Order.getDetail(this.data.meshId).then(res=>{
      this.setData({
        mesh:res.data.mesh,
        productList:res.data.list,
        address:res.data.address
      })
      var arr = []
      for(let item of res.data.list){
        arr.push(item.courierNumber)
      }
      if(arr.length > 1){
        let bool = this.isAllEqual(arr)
        this.setData({
          showAddr:bool
        })
      }
    })
  },
  goDetail(e){
    wx.navigateTo({
      url: '../../mall/proDetail/proDetail?id='+e.currentTarget.dataset.id,
    })
  },
  isAllEqual(array){
    if (array.length > 0) {
        return !array.some(function(value, index) {
            return value !== array[0];
        });
    } else {
        return true;
    }
  },

  applyRefund(e){
    wx.navigateTo({
      url: '../refundApply/refundApply?orderId='+e.currentTarget.dataset.id,
    })
  },
  refundDetail(e){
    wx.navigateTo({
      url: '../refundDetail/refundDetail?orderId='+e.currentTarget.dataset.id,
    })
  },
  comment(e){
    wx.navigateTo({
      url: '../commentEdit/commentEdit?orderId='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})